﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;

namespace ProductSpecViewer
{
    partial class Phone
    {
        private decimal priceField;
        /// <summary>
        /// Latest price.
        /// </summary>
        public decimal Price
        {
            get { return this.priceField; }
        }
        /// <summary>
        /// Save to output file. Phone model as a file name.
        /// </summary>
        public void Save()
        {
            SaveToFile(this.Name.Replace(" ", "") + ".xml");
        }
        /// <summary>
        /// TODO: Bind other phone properties at runtime.
        /// </summary>
        /// <remarks>
        /// For those not included in serialization.
        /// </remarks>
        public void Load()
        {
            //bind image into stream.
            foreach (Photo photo in this.imagesField)
            {
                string location = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
                string fullPath = location + Path.DirectorySeparatorChar + this.Name.Replace(" ", "") + Path.DirectorySeparatorChar + photo.Uri;
                photo.Image = Image.FromFile(fullPath);
            }

            //get the latest price
            var v = from f in this.pricingHistoryField
                    orderby f.Since descending
                    select f.Price;
            this.priceField = v.First();
        }
        /// <summary>
        /// True if it is a image file otherwise false.
        /// </summary>
        /// <param name="extension"></param>
        /// <returns></returns>
        private bool IsImage(string extension)
        {
            string[] ext = new string[] { ".jpg", ".bmp", ".gif", ".ttf" };
            foreach (string s in ext)
            {
                if (extension.CompareTo(s) == 0)
                    return true;
            }

            return false;
        }
    }
}